package ninja.awesome.framework.control;

import java.util.List;

import ninja.awesome.framework.Game;
import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.framework.Input.TouchEvent;
import ninja.awesome.game.Assets;
import ninja.awesome.game.Utils;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;

public class GameButton extends Widget {
	private static final String TAG = GameButton.class.getSimpleName();
	private String text = "Empty";
	private int textColor = Color.BLACK;
	private int textSize = 60;
	private int pressedColor = Color.BLUE;
	public ButtonListener listener;
	private Paint paint;
	private ButtonState state;
	private int keyCode;
	private Image pressedImage = null;

	private enum ButtonState {
		NORMAL, PRESSED, DISABLED,
	}

	public GameButton(Game game) {
		super(game);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextAlign(Align.CENTER);
		state = ButtonState.NORMAL;
	}

	public void setButtonListener(ButtonListener listener) {
		this.listener = listener;
	}

	public static interface ButtonListener {
		public void onExecute(GameButton button);

		public void onPressed(GameButton button);

		public void onRealeased(GameButton button);
	}

	@Override
	public void update(float deltaTime, List<TouchEvent> touchEvents) {
		super.update(deltaTime, touchEvents);
		int len = touchEvents.size();
		if (len == 0) {
			return;
		}
		int intersectCount = 0;
		boolean isPressed = false;
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (Utils.inBounds(event, getX(), getY(), getWidth(), getHeight())) {
				intersectCount++;
				switch (event.type) {
				case TouchEvent.TOUCH_DOWN:
				case TouchEvent.TOUCH_HOLD:
				case TouchEvent.TOUCH_DRAGGED:
					isPressed = true;
					break;
				case TouchEvent.TOUCH_UP:
					isPressed = false;
					break;
				default:
					break;
				}
			}
		}
		if (intersectCount == 0) {
			if (state == ButtonState.PRESSED) {
				state = ButtonState.NORMAL;
				if (listener != null) {
					listener.onRealeased(this);
				}
			}
		} else {
			if (state == ButtonState.NORMAL && isPressed) {
				state = ButtonState.PRESSED;
				if (listener != null) {
					listener.onPressed(this);
				}
			} else {
				if (state == ButtonState.PRESSED) {
					if (isPressed) {
						if (listener != null) {
							listener.onPressed(this);
						}
					} else {
						state = ButtonState.NORMAL;
						if (listener != null) {
							listener.onExecute(this);
							listener.onRealeased(this);
						}
					}
				}
			}
		}

	}

	@Override
	public void paint(float deltaTime) {
		Graphics g = game.getGraphics();
		if (backgroundImage == null) {
			super.paint(deltaTime);
			int backgroundColor = Color.TRANSPARENT;
			switch (state) {
			case PRESSED:
				textColor = Color.WHITE;
				backgroundColor = pressedColor;
				break;
			case NORMAL:
				textColor = Color.BLACK;
				backgroundColor = this.backgroundColor;
				break;
			case DISABLED:
				textColor = Color.GRAY;
				backgroundColor = this.backgroundColor;
				break;
			}
			g.fillRect(x, y, width, height, backgroundColor);
			g.drawRect(x, y, width, height, Color.YELLOW);
			paint.setTypeface(Assets.fontFace);
			g.drawString(text, x + width / 2, y + (int) paint.descent() + height / 2 + 4, paint);
		} else {
			switch (state) {
			case PRESSED:
				if (pressedImage !=null) {
					g.drawImage(pressedImage, getX(), getY());
				}
				break;
			default:
				g.drawImage(backgroundImage, getX(), getY());
				break;
			}
		}
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getTextColor() {
		return textColor;
	}

	public void setTextColor(int textColor) {
		paint.setColor(textColor);
		this.textColor = textColor;
	}

	public int getTextSize() {
		return textSize;
	}

	public void setTextSize(int textSize) {
		paint.setTextSize(textSize);
		this.textSize = textSize;
	}

	public int getKeyCode() {
		return keyCode;
	}

	public void setKeyCode(int keyCode) {
		this.keyCode = keyCode;
	}

	public Image getPressedImage() {
		return pressedImage;
	}

	public void setPressedImage(Image pressedImage) {
		this.pressedImage = pressedImage;
	}
}
